package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.TextVote;
import com.hebeu.gfh.pojo.po.voteResult;
import com.hebeu.gfh.pojo.vo.voteIdAndUserId;
import com.hebeu.gfh.pojo.vo.voteInfo;
import com.hebeu.gfh.pojo.vo.voteResult1;
import com.hebeu.gfh.service.textVoteService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/textVote")
public class textVoteController {
    @Autowired
    textVoteService voteService;

    //发布文字投票
    @PostMapping("/initTextVote")
    public R initTextVote(TextVote textVote){
        try {
            voteService.initTextVote(textVote);
        }catch (Exception e){
            System.out.println("捕捉异常为："+e);
            System.out.println("----------------------------------------------");
            return R.error(500,"发布文字投票失败");
        }
        return R.ok(textVote.getVoteId());
    }
    //获取已发布的投票
    @PostMapping("/getVoteList")
    public R getVoteList(int userId){
        List<TextVote> voteList = voteService.getVoteList(userId);
        return R.ok(voteList);
    }

    //删除投票
    @PostMapping("/deleteVote")
    public R deleteVote(int voteId){
        try {
            voteService.deleteVote(voteId);
        }catch (Exception e){
            System.out.println("捕捉异常为："+e);
            System.out.println("----------------------------------------------");
            return R.error(500,"删除文字投票失败");
        }
        return R.ok("删除文字投票成功");
    }
    //根据vid查询vote信息
    @PostMapping("/findVoteInfoByvid")
    public R findVoteInfoByvid(int vid){
        TextVote vote = voteService.findVoteInfoByvid(vid);
        return R.ok(vote);
    }
    //根据vid修改vote信息
    @PostMapping("/updateVoteInfo")
    public R updateVoteInfo(TextVote textVote){

        try {
            voteService.updateVoteInfo(textVote);
        }catch (Exception e){
            System.out.println("捕捉异常为："+e);
            System.out.println("----------------------------------------------");
            return R.error(500,"修改文字投票失败");
        }
        return  R.ok("修改文字投票成功");
    }

    //根据voteId获得各选项目前的票数
    @PostMapping("/voteResult")
    public R voteResult(voteIdAndUserId voteIdAndUserId){
        TextVote vote = voteService.findVoteInfoByvid(voteIdAndUserId.getVoteId());
        String options =vote.getOptions();
        List<voteResult1> voteResults = new ArrayList<>();
        String[] optionsArray = options.split(","); // 使用逗号分割字符串
        // 遍历选项数组，并将每个选项转换为VoteResult对象（初始结果为0）
        String optionsPhoto =vote.getOptionsPhoto();
        int index =0;
        if (optionsPhoto==null) {
            for (String optionStr : optionsArray) {
                String option = optionStr.trim();
                index = index + 1;
                int result = voteService.voteResult(voteIdAndUserId.getVoteId(), index);
                int selected = voteService.voteSelected(voteIdAndUserId.getVoteId(), index, voteIdAndUserId.getUserId());
                voteResults.add(new voteResult1(option, result, selected, "")); // 添加到列表中，初始结果为0

            }
        }else {
            String[] photoArray =optionsPhoto.split(",");
            for (int i = 0; i < optionsArray.length; i++) {
                String option = optionsArray[i].trim();
                String photo = photoArray[i].trim(); // 假设photoArray中的元素也需要trim
                int result = voteService.voteResult(voteIdAndUserId.getVoteId(), i + 1); // 注意索引通常从0开始，但你的service可能需要从1开始
                int selected = voteService.voteSelected(voteIdAndUserId.getVoteId(), i + 1, voteIdAndUserId.getUserId());
                // 假设voteResult1的构造函数接受选项、结果、是否选中、图片四个参数
                voteResults.add(new voteResult1(option, result, selected, photo));
            }
        }
        return  R.ok(voteResults);
    }

    //投票(目前只是单选)
    @PostMapping("/castVote")
    public R castVote(voteResult voteResult){
        int selected = voteService.voteSelected(voteResult.getVoteId(),voteResult.getUserId());
                if(selected ==0){
                    try {
                        voteService.castVote(voteResult);
                    }catch (Exception e){
                        System.out.println("异常为"+e);
                        System.out.println("=--------------------");
                        return R.error(501,"投票失败");
                    }
                }
                else{
                    return R.error(500,"已经投过票了");
                }
                return R.ok("投票成功");
    }

    //获取当前用户所在社团的所有投票
    @PostMapping("/getVoteByuserId")
    public R getVoteByuserId(int userId){
        List<voteInfo> voteInfos = voteService.getVoteByuserId(userId);
        return R.ok(voteInfos);
    }
}
